package com.xjgc.eip.twice.zxjeip2cxtld.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.xjgc.common.constant.Consants;
import com.xjgc.common.entity.ReturnResult;
import com.xjgc.common.enums.ReturnStatus;
import com.xjgc.eip.twice.zxjeip2cccjc.entity.ZxjEip2cCcjc;
import com.xjgc.eip.twice.zxjeip2cccjc.mapper.ZxjEip2cCcjcMapper;
import com.xjgc.eip.twice.zxjeip2cccjc.service.ZxjEip2cCcjcService;
import com.xjgc.eip.twice.zxjeip2cxtld.entity.ZxjEip2cXtld;
import com.xjgc.eip.twice.zxjeip2cxtld.mapper.ZxjEip2cXtldMapper;
import com.xjgc.eip.twice.zxjeip2cxtld.service.ZxjEip2cXtldService;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zhanghongbin
 * @since 2019-11-23
 */
@Service
/*@DS("proxyDs")*/
public class ZxjEip2cXtldServiceImpl extends ServiceImpl<ZxjEip2cXtldMapper, ZxjEip2cXtld> implements ZxjEip2cXtldService {

    @Autowired
    ZxjEip2cCcjcService zxjEip2cCcjcService;
    @Autowired
    ZxjEip2cXtldMapper zxjEip2cXtldMapper;
    @Autowired
    ZxjEip2cCcjcMapper zxjEip2cCcjcMapper;
    @Override
    public ReturnResult getData(Integer amount,String supplierCode) {
        ReturnResult returnResult = new ReturnResult();
        QueryWrapper<ZxjEip2cXtld> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("\"IOFLAG\"", Consants.Unread)
                .last("limit " + (amount == null ? 100 : amount));
        if(StringUtils.isNotBlank(supplierCode)){
            queryWrapper.eq("\"SUPPLIER_CODE\"",supplierCode);
        }
        try {
            List<ZxjEip2cXtld> result=super.list(queryWrapper);

            //更新查询的数据为已读
            if(result!=null&&result.size()>0){
                for (ZxjEip2cXtld zed:result) {
                    zed.setIoflag(Consants.read);
                    zed.setUpdateTime(new Date());
                }
                if (super.updateBatchById(result)){
                    returnResult.setStatus(Integer.valueOf(ReturnStatus.OK.getCode()));
                    returnResult.setData(result);
                }else {
                    returnResult.setStatus(Integer.valueOf(ReturnStatus.FAIL.getCode()));
                    returnResult.setMessage("请求异常");
                }
            }
        }catch (Exception e){
            returnResult.setStatus(Integer.valueOf(ReturnStatus.FAIL.getCode()));
            returnResult.setMessage("请求异常");
        }
        return returnResult;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateBatch(List<ZxjEip2cXtld> data) {
        boolean flag = false;
        Result<ZxjEip2cXtld> result = new Result<ZxjEip2cXtld>();
        if(data==null || data.size() == 0) {
            result.error500("参数不识别！");
        }else {
            SimpleDateFormat sdf = new SimpleDateFormat("yyy-MM-dd HH:mm:ss");
            List<String> ht = Lists.newArrayList();
            QueryWrapper<ZxjEip2cCcjc> queryWrapper=new QueryWrapper<>();
            for (ZxjEip2cXtld xtld : data){
                xtld.setAcsamplingRes("合格");
                xtld.setOpeninLoopRes("合格");
                xtld.setOpenoutLoopRes("合格");
                xtld.setCyberstormRes("合格");
                xtld.setIoflag(Consants.Unread);
                xtld.setAvalancheTestRes("合格");
                xtld.setTimingRes("合格");
                xtld.setCommunicationRes("合格");
                xtld.setSoeRes("合格");
                xtld.setCadaGarConRes("合格");
                xtld.setSoftWareFunctionRes("合格");
                xtld.setConOperationRes("合格");
                xtld.setProcessRouteNo(Consants.XTLD);
                xtld.setEndTime(new Date());
                xtld.setReviewStatus(Consants.REVIEW_STATUS3);
                ht.add(xtld.getContractNumber());
            }
            queryWrapper.in("\"CONTRACT_NUMBER\"",ht);
            ZxjEip2cCcjc ccjc = new ZxjEip2cCcjc();
            ccjc.setReviewStatus(Consants.REVIEW_STATUS3);
            zxjEip2cXtldMapper.insertBatch(data);
            zxjEip2cCcjcService.update(ccjc,queryWrapper);
            //List<String> idList = Arrays.asList(data.split(","));
            flag = true;
        }
        return flag;
    }

    @Override
    public Result<IPage<ZxjEip2cXtld>> findByCcList(ZxjEip2cXtld xtld, Integer pageNo, Integer pageSize, HttpServletRequest req) {
        Result<IPage<ZxjEip2cXtld>> result = new Result<IPage<ZxjEip2cXtld>>();
        List<ZxjEip2cXtld> ldList= zxjEip2cXtldMapper.findByCcList(xtld.getContractNumber());
        Page<ZxjEip2cXtld> page = new Page<ZxjEip2cXtld>(pageNo, pageSize);
        IPage<ZxjEip2cXtld> pageList = page.setRecords(ldList);
        result.setSuccess(true);
        result.setResult(pageList);
        return result;
    }

}
